package com.bocai.grainshop.product.service.impl;


import com.bocai.grainshop.product.entity.AttrEntity;
import com.bocai.grainshop.product.service.AttrService;
import com.bocai.grainshop.product.vo.AttrGroupWithAttrsVo;
import com.bocai.grainshop.product.vo.ItemVo;
import com.bocai.grainshop.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.BeanUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.common.utils.PageUtils;
import com.bocai.common.utils.Query;

import com.bocai.grainshop.product.dao.AttrGroupDao;
import com.bocai.grainshop.product.entity.AttrGroupEntity;
import com.bocai.grainshop.product.service.AttrGroupService;
import org.springframework.util.StringUtils;



@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long groupId) {
        //获取前端传来的检索key
        String key = (String) params.get("key");
        //封装查询条件
        QueryWrapper<AttrGroupEntity> catelog_id = new QueryWrapper<AttrGroupEntity>();
        if(StringUtils.hasText(key)){
            catelog_id.and((obj)-> obj.eq("attr_group_id",key).or().like("attr_group_name",key));
        }
        if(groupId == 0){
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    catelog_id);
            return new PageUtils(page);
        }else {
            catelog_id.eq("catelog_id", groupId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), catelog_id);
            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrByCatelogId(Long catelogId) {
        //获取此分类下所有的分组
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        //封装基本信息到vo
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(obj -> {
            AttrGroupWithAttrsVo vo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(obj, vo);
            List<AttrEntity> relationAttr = attrService.getRelationAttr(obj.getAttrGroupId());
            vo.setAttrs(relationAttr);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupwithAttrBySpuId(Long spuId, Long catalogId) {
        //查询spu所有的属性分组  以及属性分组下的所有参数信息
        List<SpuItemAttrGroupVo> attr = this.baseMapper.getAttrGroupWithAttrBySpuId(spuId,catalogId);
        return attr;
    }

}